﻿using KeaoBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace KeaoMDS.InfoManage
{
    public partial class PeiHeBiBianJi : BaseForm
    {
        public PeiHeBiBianJi()
        {
            InitializeComponent();
        }

        private int m_TabIndex = 0;
        private DataGridViewRow m_Row = null;

        public PeiHeBiBianJi(int TabIndex, DataGridViewRow Row)
        {
            InitializeComponent();
            m_TabIndex = TabIndex;
            m_Row = Row;
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                if (string.IsNullOrEmpty(txt_shuiwenliao_peibi.Text)||string.IsNullOrEmpty(cmb_shuiwenliao_cailiao.Text))
                {
                    KeaoUtils.ShowInfoMessage("信息不完整，请检查重试。");
                }
                string peibiwuliao = string.Empty;
                if (cmb_shuiwenliao_cailiao.Text.Contains("("))
                {
                    peibiwuliao = cmb_shuiwenliao_cailiao.Text.Substring(0,cmb_shuiwenliao_cailiao.Text.LastIndexOf("("));
                }
                else
                {
                    peibiwuliao = cmb_shuiwenliao_cailiao.Text;
                }

                if (m_Row == null)
                {
                    TableRow newRow = new TableRow("tbl_peihebi", Guid.NewGuid().ToString(), DBCtl.插入);
                    newRow.DataCols.Add(new TableCol() { ColName = "peibiriqi", ValueMember = date_shuiwenliao_shigongriqi.Value.ToString("yyyy-MM-dd 00:00:00"), ValueType = DataValueCommonType.日期时间 });
                    newRow.DataCols.Add(new TableCol() { ColName = "peibileixing", ValueMember = "水稳料", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "gongcheng", ValueMember = cmb_shuiwenliao_gongcheng.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "peibiwuliao", ValueMember = peibiwuliao, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "peibi", ValueMember = txt_shuiwenliao_peibi.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName="guanlianwuliao",ValueMember = cmb_shuiwenliao_cailiao.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "guanlianbianhao", ValueMember = cmb_shuiwenliao_cailiao.SelectedValue, ValueType = DataValueCommonType.字符串 });

                    if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                    {
                        KeaoUtils.ShowInfoMessage("水稳料配比保存成功。");
                        txt_shuiwenliao_peibi.Text = string.Empty;
                    }
                }
                else
                {
                    TableRow editRow = new TableRow("tbl_peihebi",string.Format("{0}",m_Row.Cells["col_shuiwenliao_id"].Value), DBCtl.修改);
                    editRow.DataCols.Add(new TableCol() { ColName = "peibiriqi", ValueMember = date_shuiwenliao_shigongriqi.Value, ValueType = DataValueCommonType.日期时间 });
                    editRow.DataCols.Add(new TableCol() { ColName = "peibileixing", ValueMember = "水稳料", ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "gongcheng", ValueMember = cmb_shuiwenliao_gongcheng.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "peibiwuliao", ValueMember = peibiwuliao, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "peibi", ValueMember = txt_shuiwenliao_peibi.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "guanlianwuliao", ValueMember = cmb_shuiwenliao_cailiao.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "guanlianbianhao", ValueMember = cmb_shuiwenliao_cailiao.SelectedValue, ValueType = DataValueCommonType.字符串 });

                    if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                    {
                        KeaoUtils.ShowInfoMessage("水稳料配比修改成功。");
                        DialogResult = DialogResult.OK;
                    }
                }
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void PeiHeBiBianJi_Load(object sender, EventArgs e)
        {
            BindCmb();

            if (m_Row != null)
            {
                tabControl1.SelectedIndex = m_TabIndex;
                if (m_TabIndex == 0)
                {
                    date_shuiwenliao_shigongriqi.Value = Convert.ToDateTime(m_Row.Cells["col_shuiwenliao_peibiriqi"].Value);
                    cmb_shuiwenliao_cailiao.SelectedValue = string.Format("{0}", m_Row.Cells["col_shuiwenliao_guanlianbianhao"].Value);
                    txt_shuiwenliao_peibi.Text = string.Format("{0}", m_Row.Cells["col_shuiwenliao_peibi"].Value);
                    cmb_shuiwenliao_gongcheng.Text = string.Format("{0}", m_Row.Cells["col_shuiwenliao_gongcheng"].Value);
                    cmb_shuiwenliao_peibidan.Enabled = false;
                    btn_peibidan.Enabled = false;
                }
            }
        }

        private void BindCmb()
        {
            Dictionary<string, string> leftTabs1 = new Dictionary<string, string>();
            leftTabs1.Add("tbl_xinghao", "tbl_xinghao.shuiwenliao=tbl_zidian.xiangmu"); 
            string createCondition = KeaoUtils._DB.CreateAndCondition("tbl_zidian.fenzu", DataValueCommonType.字符串, "水稳料", "", SearchConditionType.等于);
            DataTable Dt1 = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian", "xiangmu,xinghao,tbl_xinghao.id", leftTabs1, createCondition, "", null);
            for (int i = 0; i < Dt1.Rows.Count; i++)
            {
                DataRow row = Dt1.Rows[i];
                string rowXingHao = string.Format("{0}", row["xinghao"]);
                if (!string.IsNullOrEmpty(rowXingHao))
                {
                    row["xinghao"] = string.Format("{0}({1})", row["xiangmu"], row["xinghao"]);
                }
                else
                {
                    if (row["xiangmu"].Equals("水"))
                    {
                        row["id"] = "shui";
                        row["xinghao"] = string.Format("{0}", row["xiangmu"]);
                    }
                    else
                    {
                        Dt1.Rows.Remove(row);
                        i--;
                    }

                }
            }
            cmb_shuiwenliao_cailiao.ValueMember = "id";
            cmb_shuiwenliao_cailiao.DisplayMember = "xinghao";
            cmb_shuiwenliao_cailiao.DataSource = Dt1;

            createCondition = KeaoUtils._DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "路基", "", SearchConditionType.等于);
            DataTable GongCheng1 = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian", createCondition);
            cmb_shuiwenliao_gongcheng.DisplayMember = "xiangmu";
            cmb_shuiwenliao_gongcheng.ValueMember = "xiangmu";
            cmb_shuiwenliao_gongcheng.DataSource = GongCheng1;

            createCondition = KeaoUtils._DB.CreateAndCondition("leixing", DataValueCommonType.字符串, "水稳料", "", SearchConditionType.等于);
            DataTable PeiBiDan1 = KeaoUtils._DB.GetDataTableByCondition("tbl_peibidan","mingcheng",null, createCondition,"mingcheng",null);
            cmb_shuiwenliao_peibidan.DisplayMember = "mingcheng";
            cmb_shuiwenliao_peibidan.ValueMember = "mingcheng";
            cmb_shuiwenliao_peibidan.DataSource = PeiBiDan1;
            
        }
        private void btn_peibidan_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cmb_shuiwenliao_gongcheng.Text))
            {
                KeaoUtils.ShowInfoMessage("请设定分项工程后重试。");
                return;
            }
            //应用配比单
            if (KeaoUtils.ShowQuestionMessage(string.Format("水稳料【{0}】配合比要应用该配比单吗？",cmb_shuiwenliao_gongcheng.Text)) == DialogResult.Yes)
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("leixing", DataValueCommonType.字符串, "水稳料", "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("mingcheng", DataValueCommonType.字符串, cmb_shuiwenliao_peibidan.Text, "", SearchConditionType.等于);
                DataTable PeiBeiDt = KeaoUtils._DB.GetDataTableByCondition("tbl_peibidan", createCondition);
                foreach (DataRow row in PeiBeiDt.Rows)
                {
                    TableRow newRow = new TableRow("tbl_peihebi", Guid.NewGuid().ToString(), DBCtl.插入);
                    newRow.DataCols.Add(new TableCol() { ColName = "peibiriqi", ValueMember = date_shuiwenliao_shigongriqi.Value, ValueType = DataValueCommonType.日期时间 });
                    newRow.DataCols.Add(new TableCol() { ColName = "peibileixing", ValueMember = "水稳料", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "gongcheng", ValueMember = cmb_shuiwenliao_gongcheng.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "peibiwuliao", ValueMember = row["peibiwuliao"], ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "peibi", ValueMember = row["peibi"], ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "guanlianwuliao", ValueMember = row["guanlianwuliao"], ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "guanlianbianhao", ValueMember = row["guanlianbianhao"], ValueType = DataValueCommonType.字符串 });
                    KeaoUtils._DB.SaveTableRows(newRow);
                }

                DialogResult = DialogResult.OK;
            }
        }
    }
}
